package com.blue.blog.api.service.impl;

import com.blue.blog.api.mapper.BlogMapper;
import com.blue.blog.api.pojo.Blog;
import com.blue.blog.api.service.BlogService;
import com.blue.blog.api.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogMapper mapper;

    @Autowired
    private TagService service;

    @Override
    public List<Blog> findAllBlog() {
        return mapper.findAllBlog();
    }

    @Override
    public Blog findBlogById(Long id) {
        return mapper.findBlogById(id);
    }

    @Override
    public Integer saveBlog(Blog blog) {
        Integer result = mapper.saveBlog(blog);

        Long blogId = mapper.findNewBlogId();

        List<Long> tagIds = blog.getTagIds();
        for (Long tagId : tagIds) {
            service.saveBlogTag(blogId, tagId);
        }

        return result;
    }

    @Override
    public Integer updateBlog(Blog blog) {
        return mapper.updateBlog(blog);
    }

    @Override
    public Integer deleteBlogById(Long id) {
        return mapper.deleteBlogById(id);
    }
}
